//
//  SWHistoryViewController.m
//  ShiWu
//
//  Created by  on 2022/7/27.
//

#import "SWHistoryViewController.h"
#import "SWTypeCell.h"
#import "SWDetailViewController.h"

@interface SWHistoryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIButton *zhiwu;
@property (weak, nonatomic) IBOutlet UIButton *dongwu;
@property (weak, nonatomic) IBOutlet UIButton *guoshu;
@property (weak, nonatomic) IBOutlet UIButton *qita;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenterX1;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenterX2;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenterX3;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenterX4;
@property (weak, nonatomic) IBOutlet UIView *line;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray*array;
@end

@implementation SWHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.backBtn.hidden = YES;
    [self.collectionView registerNib:[SWTypeCell nib] forCellWithReuseIdentifier:[SWTypeCell ID]];
    if (@available(iOS 11, *))
    {
        self.collectionView.contentInsetAdjustmentBehavior  = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
   
    

    // Do any additional setup after loading the view from its nib.
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.array = [NSMutableArray array];
    for (int i = 1; i < 5; i++) {
        NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"type"),bg_sqlValue(@(i))];
        NSArray* resultArr = [SWModel bg_find:@"SWModel" where:where];
        [self.array addObject:resultArr];
    }
    [self.collectionView reloadData];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    for (UIControl *control in [self.view subviews]) {
        if ([control isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)control;
            if (btn.tag == sender.tag) {
                btn.selected = YES;
                self.lineCenterX1.active = btn.tag == 200;
                self.lineCenterX2.active = btn.tag == 201;
                self.lineCenterX3.active = btn.tag == 202;
                self.lineCenterX4.active = btn.tag == 203;
                [self.collectionView  setContentOffset:CGPointMake((SCREEN_WIDTH) * (btn.tag - 200), 0) animated:YES];
            }else{
                btn.selected = NO;
            }
        }
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  4;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    __weak SWHistoryViewController *weakSelf = self;
    SWTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[SWTypeCell ID] forIndexPath:indexPath];
    cell.array = self.array[indexPath.row];
    cell.block = ^(SWModel * _Nonnull model) {
        SWDetailViewController *vc = [[SWDetailViewController alloc] init];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    };
    return  cell;
}
#pragma mark - UICollectionViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int  index = scrollView.contentOffset.x / (SCREEN_WIDTH);
    UIButton *btn = [self.view viewWithTag:200 + index];
    [self performSelector:@selector(actionForBtn:) withObject:btn];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - TabBarHight - NavHeight - 50);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end
